package thread;

public class CachePaddingMore {
    private static class Padding {
        // 每个long类型的变量占用8个字节，下面的p1~p7一共占用56个字节
        public volatile long p1, p2, p3, p4, p5, p6, p7;
    }

    private static class T extends Padding{
        // 这个地方占用8个字节
        public volatile long x = 0L;
    }

    public static T[] array = new T[2];

    // 这个时候，array[0]和array[1]必然不可能在同一个cache line内
    static {
        array[0] = new T();
        array[1] = new T();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (long i = 0; i < 1000_0000L; i++) {
                array[0].x = i;
            }
        });

        Thread t2 = new Thread(() -> {
            for (long i = 0; i < 1000_0000L; i++) {
                array[1].x = i;
            }
        });

        final long start = System.nanoTime();

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println((System.nanoTime() - start) / 100_0000);
    }
}
